package com.mt.api.modules.controller;

import com.mt.log.annotation.Log;
import com.mt.api.modules.domain.bo.*;
import com.mt.api.modules.domain.vo.AppOrderGroupCargoVo;
import com.mt.api.util.ParamsValid;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.api.modules.service.AppOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * @author plover
 * @date 2020-07-06
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "order管理")
@RequestMapping("/app/order")
public class AppOrderController {

    private final AppOrderService orderService;

    @GetMapping("/page")
    @Log("分页查询品牌下的订单")
    @ApiOperation(value = "", notes = "配货单分页列表接口", httpMethod = "GET")
    public WebResponse pageOrder(AppOrderListBo appOrderListBo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("pageNum不能为空", appOrderListBo.getPageNum())
                .validNotNull("pageSize不能为空", appOrderListBo.getPageSize());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.pageOrder(appOrderListBo);
    }

    @GetMapping("/getOrderPage")
    @ApiOperation(value = "", notes = "配货单分页列表接口", httpMethod = "GET")
    public WebResponse getOrderPage(AppOrderListBo appOrderListBo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("pageNum不能为空", appOrderListBo.getPageNum())
                .validNotNull("pageSize不能为空", appOrderListBo.getPageSize());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.getOrderPage(appOrderListBo);
    }

    @GetMapping("/detail")
    @Log("查询订单详情")
    @ApiOperation(value = "", notes = "配货单详情接口", httpMethod = "GET")
    public WebResponse listOrder(Integer orderId) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("请输入订单id", orderId);
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.orderDetail(orderId);
    }

    @PostMapping("/add")
    @Log("增加销售单")
    // @RepeatReqValid
    @ApiOperation(value = "", notes = "配货单提交接口", httpMethod = "POST")
    public WebResponse addOrder(@RequestBody AppOrderAddBo resources) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("请选择客户", resources.getCustomerId())
                .validList("请选择销售商品", resources.getOrderDetailData());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.addOrder(resources);
    }

    @PostMapping("/edit")
    @Log("销售单修改")
    // @RepeatReqValid
    @ApiOperation(value = "", notes = "销售单修改接口", httpMethod = "POST")
    public WebResponse editOrder(@RequestBody AppOrderEditBo resources) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("请输入id", resources.getId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.editOrder(resources);
    }

    @PostMapping("/partOutStore")
    @Log("销售单部分出库")
    @ApiOperation(value = "", notes = "销售单部分出库接口", httpMethod = "POST")
    public WebResponse partOutStore(@RequestBody AppOrderOutStoreBo resources) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("销售单不存在", resources.getId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.partOutStore(resources);
    }

    @PostMapping("/completeOutStore")
    @Log("销售单完成")
    @ApiOperation(value = "", notes = "配货单完成接口", httpMethod = "POST")
    public WebResponse completeOutStore(@RequestBody AppOrderFinishBo resources) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("销售单不存在", resources.getId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.completeOutStore(resources);
    }


    @PostMapping("/cancel")
    @Log("配货取消")
    @ApiOperation(value = "", notes = "配货单取消接口", httpMethod = "POST")
    public WebResponse cancelOrder(@RequestBody AppOrderFinishBo resources) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("订单不存在", resources.getId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.cancelOrder(resources);
    }

    @PostMapping("/print")
    @Log("配货打印")
    @ApiOperation(value = "", notes = "配货单打印接口", httpMethod = "POST")
    public WebResponse printOrder(@RequestBody AppOrderPrintBo param) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("订单不存在", param.getId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.printOrder(param);
    }

    @ApiOperation(value = "", notes = "配货单确认页面接口", httpMethod = "GET")
    @GetMapping("/getOrderDetail")
    public WebResponse<AppOrderGroupCargoVo> getOrderDetail(Integer orderId) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("orderId", orderId);
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return orderService.getOrderDetail(orderId);
    }

}